/**
 * jims
 */
package com.jims.master.modules.patient.myFamily.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;

import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.patient.myFamily.dao.MyFamilyDao;
import com.jims.master.modules.patient.myFamily.entity.MyFamily;
import com.jims.master.modules.sys.utils.netease.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Service
 *
 * @author jicunteng
 * @version 2017-07-21
 */
@Service
@Transactional(readOnly = true)
public class MyFamilyService extends CrudService<MyFamilyDao, MyFamily> {


    /**
     * 根据用户id查找他的家人信息
     *
     * @param
     * @return
     */
    public Page<MyFamily> findFamilyById(Page<MyFamily> page, Map map) {
        page.setPageNo(Integer.parseInt((String) map.get("pageNo")));
        if(StringUtils.isNotBlank((String) map.get("pageSize"))){
            page.setPageSize(Integer.parseInt((String) map.get("pageSize")));
        }else{
            page.setPageSize(10);
        }
        MyFamily myFamily = new MyFamily();
        myFamily.setPersonId((String)map.get("personId"));
        myFamily.setPage(page);
        page.setList(this.dao.findFamilyById(myFamily));
        return page;
    }

    /**
     * 根据id删除他的家人信息
     *
     * @param id
     * @return
     * @author jicunteng
     */
    @Transactional(readOnly = false)
    public int delete(String id) {
        int i = dao.delete(id);
        return i;
    }

    /**
     * 上传身份证
     *
     * @param file
     * @return
     * @throws Exception
     * @author jicunteng
     */
    public String savePic(MultipartFile file, HttpServletRequest request) throws Exception {
        String home = request.getSession().getServletContext().getRealPath("");
        String pathImg = "idCard";
        String datePath = DateUtils.formatDate(new Date(), "yyyy/MM/dd");
        //图片保存路径
        String pathUrl = File.separator + pathImg + File.separator + datePath + File.separator + "idCard";
        String str = UUIDUtil.getUUID();
        String headPath = str + ".";
        //重新命名文件
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
        pathUrl = pathUrl + File.separator + headPath + fileName;
        File fileTemp = new File(home + pathUrl);
        if (!fileTemp.exists())
            fileTemp.mkdirs();
        //上传图片
        file.transferTo(fileTemp);
        //文件名为当前时间
        return pathUrl;
    }

    /**
     * 根据id更新电话
     *
     * @param id
     * @return
     * @author jicunteng
     */
    public int updatePhoneById(String mobilePhone, String id) {
        int i = dao.updatePhoneById(mobilePhone, id);
        return i;
    }

    /**
     * 根据电话查找用户家人信息
     *
     * @param mobilePhone
     * @return
     * @author jicunteng
     */
    public MyFamily findByMobliePhone(String mobilePhone) {
        MyFamily myFamily = dao.findByMobliePhone(mobilePhone);
        return myFamily;
    }

    /**
     * 根据身份证号码查找用户家人信息
     *
     * @param idCard
     * @return
     * @author jicunteng
     */
    public MyFamily getMyFamilyByIdCard(String idCard) {
        MyFamily myFamily = dao.getMyFamilyByIdCard(idCard);
        return myFamily;
    }
    /**
     * 判断亲属关系是否存在
     * @param myFamily
     * @return
     */
    public int judgeRelationship(MyFamily myFamily){
        return dao.judgeRelationship(myFamily);
    }
}